﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using myTwitter.Commands;

namespace myTwitter.BO
{
    public class TrendTopic
    {
        public string Name { get; set; }
        public string Url { get; set; }
        public SearchTrendCmd SearchTrendCommand { get; set; }

        public TrendTopic(string rawTrend)
        {
            if (IsValidTrend(rawTrend))
            {
                int nameEndIndex = rawTrend.IndexOf(@""",""url"":");
                int urlStartIndex = rawTrend.IndexOf(":",rawTrend.IndexOf(@""",""url"":"))+2;
                Name = rawTrend.Substring(9, nameEndIndex - 9);
                Url = rawTrend.Substring(urlStartIndex, rawTrend.LastIndexOf(@"""") - urlStartIndex);
            }
            SearchTrendCommand = new SearchTrendCmd(this);
        }


        private bool IsValidTrend(string rawTrend)
        {
            if (!string.IsNullOrEmpty(rawTrend))
            {
                if (rawTrend.StartsWith("{") && rawTrend.EndsWith("}"))
                {
                    if (rawTrend.IndexOf("name") > 0 && rawTrend.IndexOf("url") > 0 && rawTrend.IndexOf(",") > 0)
                        return true;
                }
            }
            return false;
        }
    }
}
